<template>
  <div class="app-container">
    <el-card class="report-card">
      <el-form :inline="true" class="report-form">
        <el-form-item label="日期范围">
          <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="generatePdfReport">生成PDF报告</el-button>
          <el-button type="success" :disabled="!pdfUrl" @click="downloadPdf">下载PDF</el-button>
        </el-form-item>
      </el-form>

      <div v-if="pdfUrl" class="pdf-preview-container">
        <div class="pdf-preview-wrapper">
          <VuePdfEmbed v-if="showPdf" :source="pdfUrl" :page="1" :width="800" @error="handlePdfError" />
          <el-empty v-if="pdfError" description="PDF加载失败" />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import VuePdfEmbed from 'vue-pdf-embed'
import request from "@/utils/request";

const dateRange = ref([])
const pdfUrl = ref('')
const showPdf = ref(false)
const pdfError = ref(false)

const handlePdfError = () => {
  pdfError.value = true
  showPdf.value = false
  ElMessage.error('PDF加载失败')
}

/**
 * 获取PDF报告预览
 */
const generatePdfReport = async () => {
  if (!validateDateRange()) return

  try {
    pdfError.value = false
    showPdf.value = false
    const response = await request({
      url: '/api/v1/reports/pdf',
      method: 'get',
      params: {
        startDate: dateRange.value[0],
        endDate: dateRange.value[1]
      },
      responseType: 'blob'
    })

    const blob = new Blob([response.data], { type: 'application/pdf' })
    pdfUrl.value = URL.createObjectURL(blob)
    showPdf.value = true
  } catch (error) {
    ElMessage.error('生成PDF报告失败')
  }
}

/**
 * 验证日期
 */
const validateDateRange = () => {
  if (!dateRange.value || dateRange.value.length !== 2) {
    ElMessage.warning('请选择日期范围')
    return false
  } else if (new Date(dateRange.value[0]) > new Date(dateRange.value[1])) {
    ElMessage.warning('开始日期不能大于结束日期')
    return false
  }
  return true
}

/**
 * 下载PDF
 */
const downloadPdf = () => {
  if (!pdfUrl.value) return

  const link = document.createElement('a')
  link.href = pdfUrl.value
  link.download = `回收报告_${dateRange.value[0]}_${dateRange.value[1]}.pdf`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.report-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.report-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.pdf-preview-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 500px;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.pdf-preview-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: 800px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: white;
  padding: 20px;
  border-radius: 4px;
}

:deep(.el-empty) {
  padding: 40px 0;
}
</style>
